<?php
namespace app\personal\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
    /**
     *   初始化
     *  判断是否登陆
     */
    public function _initialize()
    {
        if (session('tel') == null){
            return $this->error('您还未登陆,请先登陆!');
        }
    }
    /**
     * 显示收藏列表
     *
     *@return user/index
     */
    public function index()
    {
        $house = Db::table(['article', 'user_self', 'house'])
            ->field('article.amage,house.id,article.aid,article.aname, article.regtime,user_self.nickname')
            ->where('house.aid=article.aid and article.uid=user_self.uid and house.uid ='.session('uid'))
            ->select();
//        dump($house);
        //分类
            $ass = Db::name('subscribe s')
                ->join('column c','s.cid = c.cid')
                ->where('s.uid ='.session('uid'))
                ->limit(9)
                ->select();
        $user = Db::table('user_self')->field('nickname,icon')->where('uid ='.session('uid'))->find();
        return view('Index/index', [
            'title' => '【众评网】为你私人定制的资讯客户端 --- 【che-cn.com】 - 个人中心',
            'house' => $house,
            'ass' => $ass,
            'user' => $user,
        ]);
    }

    /**
     * 取消收藏
     * @param $id  收藏id
     * @return \think\response\Json
     */
    public function delete($id)
    {
        $result = Db::table('house')->where('uid ='.session('uid'))->delete($id);
        if (!$result > 0){
            return $this->error('取消收藏失败');
        }
        return json($result);
    }

    /**
     * 添加收藏
     * @param $id  文章 id
     * @return \think\response\Json
     */
    public function create($id)
    {
        $data = [
            'uid' => session('uid'),
            'aid' => $id,
            'time' => time()
        ];
//        dump($data);die;
        $result = Db::table('house')->data($data)->insert();
        return json($result);
    }

}